﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.Common
{
    public class WebHelper
    {
        public static string PostData(string url,string parameter)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            RestClient client = new RestClient { BaseUrl = url, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36" };
            byte[] data = encoding.GetBytes("" + "&dataFrom=\"\"&epCheck=\"false\"");
            RestRequest request = new RestRequest(Method.POST);
            //request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Content-Length", (data.Length + 2540).ToString());
            request.AddHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Accept-Language", "zh-cn");
            request.AddHeader("X-Requested-With", "XMLHttpRequest");
            request.AddHeader("postman-token", Guid.NewGuid().ToString());
            request.AddHeader("cache-control", "no-cache");
            //request.AddHeader("accessToken", accessToken);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"postdata\"\r\n\r\n" + parameter + "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);



            IRestResponse response = null;
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    response = client.Execute(request);
                    break;
                }
                catch (Exception ex)
                {
                   
                    Console.WriteLine(ex.Message);
                }
            }
            if (response != null)
            {
                return response.Content;
            }
            return null;

        }

        public static string GetDataParameter(string url, string parameter)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            RestClient client = new RestClient { BaseUrl = url, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36" };
            byte[] data = encoding.GetBytes("" + "&dataFrom=\"\"&epCheck=\"false\"");
            RestRequest request = new RestRequest(Method.POST);
            //request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Content-Length", (data.Length + 540).ToString());
            request.AddHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Accept-Language", "zh-cn");
            request.AddHeader("X-Requested-With", "XMLHttpRequest");
            request.AddHeader("postman-token", Guid.NewGuid().ToString());
            request.AddHeader("cache-control", "no-cache");
            //request.AddHeader("accessToken", accessToken);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json",  parameter , ParameterType.RequestBody);

          

            IRestResponse response = null;
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    response = client.Execute(request);
                    break;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            if (response != null)
            {
                return response.Content;
            }
            return null;

        }



        public static string GetData(string url, string parameter)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            RestClient client = new RestClient { BaseUrl = url, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36" };
            byte[] data = encoding.GetBytes("" + "&dataFrom=\"\"&epCheck=\"false\"");
            RestRequest request = new RestRequest(Method.POST);
            //request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Content-Length", (data.Length + 540).ToString());
            request.AddHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Accept-Language", "zh-cn");
            request.AddHeader("X-Requested-With", "XMLHttpRequest");
            request.AddHeader("postman-token", Guid.NewGuid().ToString());
            request.AddHeader("cache-control", "no-cache");
            //request.AddHeader("accessToken", accessToken);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", "\"" + parameter + "\"\n", ParameterType.RequestBody);

            //var client = new RestClient("http://localhost:8753/api/message/ConnectGetTonkey");
            //var request = new RestRequest(Method.POST);
            //request.AddHeader("postman-token", "e5ea0a5b-a14f-700c-8786-099d39b47309");
            //request.AddHeader("cache-control", "no-cache");
            //request.AddHeader("content-type", "application/json");
            //request.AddParameter("application/json", "\"ADB9491C17571143C3FFF8E48C1D0449E357736B9F65A3D8C4FC2463FABCACA7C3AFAA58D8AB20850DDCE3F287B613E30DC5D1888505B8BBFAC980780733BC9F47CEC2E660324A8BD07C4A1243732D15911547354E8A3EC9\"\n", ParameterType.RequestBody);
            //IRestResponse response = client.Execute(request);

            IRestResponse response = null;
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    response = client.Execute(request);
                    break;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            if (response != null)
            {
                return response.Content;
            }
            return null;

        }
     
    }
}
